
import UIKit


class TF_ChatRecordCell: UITableViewCell, UITextViewDelegate {
    /** 代理对象 */
    @objc public weak var delegate : TF_ChatRecordCellDelegate?
    @objc public var cellWidth:CGFloat = 0

    let avatarImageView = UIImageView()
    let image = UIImageView()
    let bgView = UIView()
    let content = TF_StoyCopyTextView.init()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = UIColor.clear
            
        self.bgView.isUserInteractionEnabled = true
        self.contentView.addSubview(self.bgView)
       
        //头像
        self.avatarImageView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
  
        self.bgView.addSubview(self.avatarImageView)
       
        //聊天内容
        
        self.content.textContainerInset =  UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        self.content.isEditable = false

        
        self.content.backgroundColor = UIColor.clear
        self.content.isScrollEnabled = false
        self.content.textContainer.lineFragmentPadding = 0;
    
        self.content.font = UIFont.PingFang(size: 16)
        
        self.content.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(red: 28/255.0, green: 247/255.0, blue: 255/255.0, alpha: 1.0)]

        self.bgView.addSubview(self.content)

    
        self.image.backgroundColor = UIColor.clear
        self.image.isUserInteractionEnabled = true
        self.bgView.clipsToBounds = true
        self.bgView.addSubview(self.image)
        self.image.contentMode = .scaleToFill;
            
        let singleRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(self.dealSingleTap))
        /*单击    -------    点击的次数*/
        singleRecognizer.numberOfTapsRequired = 1;
        self.image.addGestureRecognizer(singleRecognizer)
        

        
    }
    @objc func dealSingleTap()
    {
        if self.delegate != nil && (self.delegate?.responds(to: #selector(TF_ChatRecordCellDelegate.fullScreenPicture(_:))))!{

            self.delegate?.fullScreenPicture(self.model!)
        }
    }
    
    @objc public var model: TF_LivingChatData?
    {
        didSet {
            if ((model) != nil)
            {
        
                //学员与游客
                if (model!.role == .roleeUser || model!.role == .roleeGuest) {
                    self.bgView.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.3)
                    

                }else{
                    self.bgView.backgroundColor = UIColor(red: 150/255.0, green: 106/255.0, blue: 255/255.0, alpha: 0.50)
                  
                }
                self.bgView.layer.cornerRadius = 21
                let url:URL? = URL.init(string: model!.avatar as String)
                self.avatarImageView.sd_setImage(with: url, completed: nil)

                let msg:NSString = model!.nickname + "：" as NSString
            
                let imageURLSttr = model!.imageURL as NSString
                
                var new1 = "";
                if imageURLSttr.length>0 {
                    
                }else{
                    new1 = (model!.msg as NSString) as String
                }
                //如果是url, 聊天数据就为空
                let nwwMsg:NSString = (msg as String) + new1  as NSString
                
                let attributedText =  TalkfunSharedTools.getAttributedText(nwwMsg as String, fontSize: UIFont.PingFang(size: 16), foregroundColor: UIColor.white)
               
            let replyList = model!.replyList as NSArray
            let reversedArray = replyList.reverseObjectEnumerator().allObjects
                for i in stride(from: 0, to: reversedArray.count ,by: 1) {

                    let reply = reversedArray[i] as! TFChatReply
                        var aa = "" as NSString
                    aa = (aa as String) + "@" as NSString
                    aa = (aa as String) + reply.nickname as NSString
                    aa = (aa as String) + " " as NSString
                    
                    
                    let attrsSize: CGSize = aa.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 16)])
                        
                        let aaL = UILabel()
                        aaL.frame = CGRect(x: 0, y: 0, width: attrsSize.width+2, height: attrsSize.height+2)
                        aaL.text = aa as String
                        aaL.font = UIFont.PingFang(size: 16)
                      
                    if (reply.isMe) {
                        aaL.textColor = UIColor.white
                        aaL.backgroundColor = UIColor(red: 65/255.0, green: 255/255.0, blue: 242/255.0, alpha: 1.0)
                        
                    }else{
                        aaL.backgroundColor = UIColor.clear
                        aaL.textColor = UIColor(red: 65/255.0, green: 255/255.0, blue: 242/255.0, alpha: 1.0)
                    }
                        
                        aaL.clipsToBounds = true
                        aaL.layer.cornerRadius = 4;
                        aaL.textAlignment = .center;
                    
                        //调用方法，转化成Image
                    let image = TalkfunSharedTools.imageWithUIView(aaL)
                        //创建Image的富文本格式
                        let attach = NSTextAttachment()
                        attach.bounds = CGRect(x: 0, y: -(aaL.bounds.size.height/4), width: aaL.bounds.size.width, height: aaL.bounds.size.height)
                   //这个-2.5是为了调整下标签跟文字的位置
                        attach.image = image;
                        //添加到富文本对象里
                
                        let  imageStr = NSAttributedString.init(attachment: attach)
                    
                    //加入文字前面
                    attributedText.insert(imageStr, at: msg.length)
                    
                }
                      

                let font16 = UIFont.PingFang(size: 16)
                var foregroundColor = UIColor.white
                if (model!.role == .roleeUser || model!.role == .roleeGuest) {
                    foregroundColor = UIColor(red: 255/255.0, green: 205/255.0, blue: 68/255.0, alpha: 1.0)

                 }else{
                     foregroundColor = UIColor(red: 65/255.0, green: 255/255.0, blue: 242/255.0, alpha: 1.0)

                 }
                  

              

                self.content.addHyperlink(font: font16,attributedText: attributedText)
                self.content.attributedText = attributedText
                
                let contentDict = TalkfunSharedTools.getSizeLabel(labelString: attributedText, labelWidth:self.cellWidth-42-12, labelfont: UIFont.PingFang(size: 16))
                
                
                
                self.content.frame = CGRect(x: 42, y: 9, width: contentDict.width, height: contentDict.height)
                
                let backgroundColorViewWidth:CGFloat = 42 + 12 + contentDict.width
                var backgroundColorViewHeight:CGFloat = contentDict.height + 10 + 10
                if (backgroundColorViewHeight<42) {
                    backgroundColorViewHeight = 42;
                }
                  
                self.avatarImageView.frame = CGRect(x: 5, y: 6, width: 30, height: 30)
            
                self.avatarImageView.layer.masksToBounds = true
                self.avatarImageView.layer.cornerRadius = self.avatarImageView.bounds.size.width / 2
                
                
                if (imageURLSttr.length>0) {
                
               var imageHight:CGFloat = 104
                   
               let imageWidth:CGFloat =  self.cellWidth - 5 - 30 - 10
                    
              var backgroundColorViewWidth:CGFloat =  self.cellWidth - 20;

                    if ((model?.image) != nil&&model!.image.size.width >= backgroundColorViewWidth) {

                        let proportion:CGFloat = (model?.image.size.width)! / imageWidth

                        imageHight = (model?.image.size.height)! / proportion;
                   }
                    
                    else if((model?.image) != nil){
                        //下载图片成功
                        imageHight = (model?.image.size.height)!;
                        backgroundColorViewWidth =  (model?.image.size.width)! + (5 + 30 + 10);
                    }
                    
                let height:CGFloat = imageHight + 36.5 + 12 + 12
                    
                self.bgView.frame = CGRect(x: 10, y: 2.5, width: backgroundColorViewWidth, height: height)
               
                let imageX:CGFloat = self.avatarImageView.frame.maxX
                
                self.image.frame = CGRect(x: imageX, y: 36.5 + 12, width: backgroundColorViewWidth - imageX - 10, height: imageHight)
                    
                self.image.image = model?.image
                    
                }
                else{
                    self.image.image = nil;
                    self.image.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
              
                    self.bgView.frame = CGRect(x: 10, y: 2.5, width: backgroundColorViewWidth, height: backgroundColorViewHeight)
            
                }
           }
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  

}

@objc protocol TF_ChatRecordCellDelegate : NSObjectProtocol {
    //全屏图片                                图片序列号
    func fullScreenPicture(_ model : TF_LivingChatData)
}
